using System;
using System.Linq;
using System.Security.Claims;

namespace HBKY.Shop.Extensions.Extensions
{
    /// <summary>
    /// Clamis  信息获取
    /// </summary>
    public static class ClamisPrincipalExtension
    {
        public static int GetUserId(this ClaimsPrincipal principal)
        {
            principal.VerifyPrincipal();
            var value = principal.FindFirst(ClaimTypes.Sid)?.Value;
            if (value != null)
                return int.Parse(value);
            return -1;
        }

        public static string GetName(this ClaimsPrincipal principal)
        {
            principal.VerifyPrincipal();
            return principal.FindFirst(ClaimTypes.Name)?.Value;
        }

        public static string GetDisplayName(this ClaimsPrincipal principal)
        {
            principal.VerifyPrincipal();
            return principal.FindFirst(ClaimTypes.Name)?.Value;
        }

        public static string GetPhone(this ClaimsPrincipal principal)
        {
            principal.VerifyPrincipal();
            return principal.FindFirst(ClaimTypes.MobilePhone)?.Value;
        }

        public static int GetLoginId(this ClaimsPrincipal principal)
        {
            principal.VerifyPrincipal();
            var value = principal.FindFirst(ClaimTypes.SerialNumber)?.Value;
            if (value != null)
                return int.Parse(value);
            return -1;
        }

        public static int GetCid(this ClaimsPrincipal principal)
        {
            principal.VerifyPrincipal();
            var value = principal.FindFirst(ClaimTypes.GroupSid)?.Value;
            if (value != null)
                return int.Parse(value);
            return -1;
        }

        public static string GetCompany(this ClaimsPrincipal principal)
        {
            principal.VerifyPrincipal();
            return principal.FindFirst(ClaimTypes.Country)?.Value;
        }

        public static string GetPosition(this ClaimsPrincipal principal)
        {
            principal.VerifyPrincipal();
            return principal.FindFirst(ClaimTypes.Role)?.Value;
        }

        public static string GetAvatar(this ClaimsPrincipal principal)
        {
            principal.VerifyPrincipal();
            return principal.FindFirst(ClaimTypes.Actor)?.Value;
        }

        /// <summary>
        /// 组织机构类型
        /// </summary>
        /// <param name="principal"></param>
        /// <returns></returns>        
        public static int[] GetRoleIds(this ClaimsPrincipal principal)
        {
            principal.VerifyPrincipal();
            var values = principal.FindAll(ClaimTypes.Role).ToArray();
            if (values.Length > 0)
                return values.Select(Convert.ToInt32).ToArray();
            return null;
        }

     
        /// ClaimsPrincipal
        /// </summary>
        /// <param name="principal"></param>
        private static void VerifyPrincipal(this ClaimsPrincipal principal)
        {
            if (principal == null) throw new ArgumentNullException(nameof(principal));
        }
    }
}